<?php
//
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$return['user_buystatus']=$this->user_buystatus;
if($_RQ['op']=='main'){
	$item=pdo_get('jy_scrm_member',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	$member=cfc('core')->member_show($_SESSION['uid']);
	$item=array_merge($member,$item);
	$item['mymembernum']=pdo_count('jy_scrm_member',array('uniacid'=>$_SESSION['uniacid'],'external_userid'=>$_SESSION['openid']));
	$item['myqunnum']=pdo_count('jy_scrm_qun',array('uniacid'=>$_SESSION['uniacid'],'owner'=>$_SESSION['openid']));	
	$item['daymembernum']=pdo_count('jy_scrm_member',array('uniacid'=>$_SESSION['uniacid'],'external_userid'=>$_SESSION['openid'],'createtime >'=>$daytime));
	$item['daygenjinnum']=pdo_count('jy_scrm_worker_linelog',array('uniacid'=>$_SESSION['uniacid'],'userid'=>$item['id'],'createtime >'=>$daytime));
	if($item['daygenjinnum']){
		$parameter=array(
				'params'=>array('uniacid'=>$_SESSION['uniacid'],'userid'=>$item['id']),
				'orderby'=>'createtime asc',
				'limit'=>'1',
		);
		$linelog=pdo_getlist('jy_scrm_worker_linelog',$parameter);
		$item['linestart']=$linelog[0]['createtime'];
		$parameter['orderby']='createtime desc';
		$linelog=pdo_getlist('jy_scrm_worker_linelog',$parameter);
		$item['lineend']=$linelog[0]['createtime'];
		$item['dayliushinum']=price(($item['lineend']-$item['linestart'])/3600);
	}else{
		$item['dayliushinum']=0;
	}
	$daytime = mktime(0,0,0,date('m'),date('d'),date('Y'));
	$link_time[0]=0;
	$link_time[1]=$daytime-3*24*3600;
	$link_time[2]=$daytime-7*24*3600;
	$link_time[3]=$daytime-15*24*3600;
	$link_time[4]=$daytime-30*24*3600;
	$link_time[5]=$daytime-90*24*3600;
	$link_time[6]=$daytime-180*24*3600;
	$link_times=$this->link_times;
	$return['optype']='log';
	if($item['genjintime']){
		$item['genjintime']=date('Y-m-d',$item['genjintime']);
	}
	if($item['nextgenjintime']){
		$item['nextgenjintime']=date('Y-m-d',$item['nextgenjintime']);
	}
	$item['tags']=json_decode($item['tags'],true);
	$return['item']=$item;
	$this->session('userid',$item['id']);
	$return['post']=array('id'=>$item['id'],'buystatus'=>$item['buystatus']?:1);
	set_html('title','我的');
	exi($return);
}
// js 获取数据
if($_RQ['op']=='log'){
	$userid=intval($_RQ['id'])?:$this->session('userid');
	if(!$userid){
		$item=pdo_get('jy_scrm_member',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
		$userid=$item['id'];
	}
	$params=array('uniacid'=>$_SESSION['uniacid'],'userid'=>$userid);
	if(isset($_RQ['istop'])){
		$params['istop']=intval($_RQ['istop']);
		$return['params']['istop']=$_RQ['istop'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('jy_scrm_worker_linelog',$params,array($pindex,$psize),$total,$fields,'','id desc');
	foreach($list as $k=>&$v){
		$v['createtime']= date('Y/m/d H:i:s',$v['createtime']);
		$v['worker']=pdo_get('jy_scrm_worker',array('id'=>$v['workerid'],'uniacid'=>$_SESSION['uniacid']),array('id','name'));	
	}
	$return['user_buystatus']=$this->user_buystatus;
	$return['list']=$list;
	exi($return);
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	$item=pdo_get('jy_scrm_member',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	if($_RQ['postdata']){
		$workerid=pdo_getcolumn('jy_scrm_worker',array('userid'=>$_SESSION['openid'],'uniacid'=>$_SESSION['uniacid']),'id');
		$postdata=get_postdata();
		$nexttime=intval(strtotime($postdata['nexttime']))?:TIMESTAMP+2*24*3600;
		$data=array(
			'content'=>trim($postdata['content']),
			'workerid'=>$workerid,
			'userid'=>$postdata['id'],
			'isout'=>1,
			'istop'=>intval($postdata['istop']),
			'nexttime'=>$nexttime,
			'createtime'=>TIMESTAMP,
		);
		$data['uniacid']=$_SESSION['uniacid'];
		pdo_insert('jy_scrm_worker_linelog',$data);
		pdo_update('jy_scrm_member',array('genjintime'=>TIMESTAMP,'nextgenjintime'=>$nexttime,'star'=>$postdata['star'],'buystatus'=>$postdata['buystatus']?:1),array('id'=>$postdata['id'],'uniacid'=>$_SESSION['uniacid']));
		exi('跟进成功！','','jy_scrm/member.item/main/id='.$postdata['id']);
	}
	$return['item']=$item;
	$return['post']=array('id'=>$id,'star'=>$item['star'],'buystatus'=>$item['buystatus']?:'1');
	exi($return);
}
?>